package net.csdn.business.common.domain.request;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import net.csdn.business.common.domain.issue.IssueActionTextEnum;
import net.csdn.business.common.domain.issue.IssueExt;
import net.csdn.business.common.domain.issue.event.BaseEventDto;
import net.csdn.business.common.enums.UserTypeEnum;
import net.csdn.business.common.utils.JsonUtil;
import org.apache.commons.lang3.ObjectUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * @author likun
 * @date 2022/6/7 18:33
 */
@Data
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
public class CommentSaveReqDto extends CommentReqDto implements Serializable {
    @NotBlank(message = "issueId 不能为空")
    @Size(min = 24, max = 24, message = "parentId 格式不正确")
    private String issueId;

    private Long projectId;

    @Size(min = 24, max = 24, message = "parentId 格式不正确")
    private String parentId;
    private String replyUser;

    /**
     * 是否子评论
     * 业务流转使用，暂时省略 context bean
     */
    private boolean isChild;

    private JSONObject param;

    private Date createTime;

    public CommentSaveReqDto(String issueId, Long projectId, String uid, String content, String mdContent, List<String> commentAts) {
        this.issueId = issueId;
        this.projectId = projectId;
        super.setUid(uid);
        super.setContent(content);
        super.setMdContent(mdContent);
        super.setCommentAts(commentAts);
    }

    public CommentSaveReqDto(String issueId, Long projectId, String uid, String content, JSONObject param, Date createTime) {
        this.issueId = issueId;
        this.projectId = projectId;
        super.setUid(uid);
        super.setContent(content);
        this.param = param;
        this.createTime = createTime;
    }

    public CommentSaveReqDto(String issueId,Date createTime, Long projectId, String uid, Integer userType, String content, String mdContent, List<String> commentAts, JSONObject param, IssueExt ext) {
        this.issueId = issueId;
        this.projectId = projectId;
        if(ObjectUtils.isNotEmpty(createTime)){
            this.createTime = createTime;
        }
        super.setUid(uid);
        if (ObjectUtils.isNotEmpty(userType)) {
            this.setUserType(UserTypeEnum.ofType(userType).getType());
        }
        super.setContent(content);
        super.setMdContent(mdContent);
        super.setCommentAts(commentAts);
        this.param = param;
        super.setIssueExt(ext);
    }

    public CommentSaveReqDto(String issueId, Long projectId, String uid, Integer userType, String content, String mdContent, List<String> commentAts, JSONObject param, IssueExt ext) {
        this.issueId = issueId;
        this.projectId = projectId;
        super.setUid(uid);
        if (ObjectUtils.isNotEmpty(userType)) {
            this.setUserType(UserTypeEnum.ofType(userType).getType());
        }
        super.setContent(content);
        super.setMdContent(mdContent);
        super.setCommentAts(commentAts);
        this.param = param;
        super.setIssueExt(ext);
    }

    public static <T extends BaseEventDto> CommentSaveReqDto buildChange(String issueId, Long projectId, String uid, String content, Date createTime, IssueActionTextEnum actionText, String ip, T param) {
        // ext 保存方便后续做语言切换
        param.setActionText(actionText);
        CommentSaveReqDto dto = new CommentSaveReqDto(issueId, projectId, uid, content, JsonUtil.toJsonObject(param, JSONWriter.Feature.WriteEnumsUsingName), createTime);
        dto.setIp(ip);
        if (ObjectUtils.isNotEmpty(param.getActionUserType())) {
            dto.setUserType(param.getActionUserType());
        } else {
            dto.setUserType(UserTypeEnum.USER.getType());
        }
        // 处理冗余 appInfo
        if (ObjectUtils.isNotEmpty(param.getExt())) {
            dto.setParam(JsonUtil.mergeJson(JsonUtil.toJsonObject(param.getExt()), dto.getParam()));
        }
        return dto;
    }
}
